﻿using System;
using System.Collections;
using System.Web.UI;
using AMS.Core.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace AMS.Web.BaseData
{
    public partial class BarcodeFormatEdit : Page
    {
        private BaseDataManager m_help = new BaseDataManager();
        PermissionManager m_PermissionManger = new PermissionManager();
        private string MenuId
        {
            get
            {

                if (ViewState["MenuId"] != null)
                    return ViewState["MenuId"].ToString();
                else
                    return "";
            }
            set { ViewState["MenuId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if(!IsPostBack)
            {
                if(Request.QueryString["Id"] != null)
                {
                    labelId.Text = Request.QueryString["Id"].Trim();
                    BarcodeFormat currentBarcodeFormat;
                    IList objlist = m_help.GetObjectList(new BarcodeFormat().GetType().Name, "Id", labelId.Text.Trim());
                    if (objlist != null && objlist.Count > 0)
                    {
                        currentBarcodeFormat = (BarcodeFormat)objlist[0];
                        ZLTextBox_Prefix.Text = currentBarcodeFormat.Prefix;
                        ddlAssetTypeNameList.Items.FindByValue(currentBarcodeFormat.AssetTypeName.Trim()).Selected = true;
                        if(currentBarcodeFormat.IsEnableDate)
                        {
                            ddlIsEnableDate.Items.FindByValue("true").Selected = true;
                        }
                        else if(currentBarcodeFormat.IsEnableDate == false)
                        {
                            ddlIsEnableDate.Items.FindByValue("false").Selected = true;
                        }
                        
                    }
                }
                 if (Request["MenuId"] != null)
                    MenuId = Request["MenuId"].Trim(); 
                 if (!m_PermissionManger.CheckPageIsCanEdit(this))
                 {
                     btnSubmit.Enabled = false;
                     btnSubmit.ToolTip = "没有权限操作此功能";
                 }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("BarcodeFormatSet.aspx?MenuId=" + MenuId, false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strPrefix = ZLTextBox_Prefix.Text.ToUpper().Trim();
            string strAssetTypeName = ddlAssetTypeNameList.SelectedValue.Trim();
            bool boolIsEnableDate = Convert.ToBoolean(ddlIsEnableDate.SelectedValue.Trim());
            bool access = false;
            if(strPrefix.Trim().Length > 10)
            {
                Alert.MessageBox("前缀不能超过10位字符!");
                return;
            }
            if(labelId.Text.Trim().Length > 0 && labelId.Text.Trim().Length == 36)
            {
                IList AssetList = m_help.GetObjectList(new AssetInfo().GetType().Name, "TypeName", strAssetTypeName);
                if(AssetList != null && AssetList.Count > 0)
                {
                    Alert.MessageBox("此条码格式已经被资产使用，不能修改！");
                    return;
                }
                IList objList = m_help.GetObjectList(new BarcodeFormat().GetType().Name, "Id", labelId.Text.Trim());
                if(objList != null && objList.Count > 0)
                {
                    var currentBarcodeFormat = (BarcodeFormat) objList[0];
                    currentBarcodeFormat.Prefix = strPrefix;
                    currentBarcodeFormat.AssetTypeName = strAssetTypeName;
                    currentBarcodeFormat.IsEnableDate = boolIsEnableDate;
                    access = m_help.SaveOrUpdate(currentBarcodeFormat);
                }
            }
            else
            {
                var currentBarcodeFormat = new BarcodeFormat();
                IList objList = m_help.GetObjectList(new BarcodeFormat().GetType().Name, "AssetTypeName", strAssetTypeName);
                if (objList != null && objList.Count > 0)
                { 
                    Alert.MessageBox("同一类型的资产只能有一种条码格式!");
                    return;
                }
                currentBarcodeFormat.Prefix = strPrefix;
                currentBarcodeFormat.AssetTypeName = strAssetTypeName;
                currentBarcodeFormat.IsEnableDate = boolIsEnableDate;
                access = m_help.SaveOrUpdate(currentBarcodeFormat);
            }

            if(access)
            {
                Alert.MessageBox("保存成功!");
            }
        }
    }
}
